<script type="text/javascript">
$(document).ready(function() {
	//$('li.node').addClass('close');
	//$('li.node').children('ul').hide();
	//$('li.node').each(
	//	function(column) {
	//		$(this).mouseenter(function(event){
	//			if (this == event.target && $(this).is('.close')) {
	//				$(this).children('ul').show();
	//				$(this).removeClass('close');
	//				$(this).addClass('open');
	//			}
	//		});
	//		$(this).mouseleave(function(event) {
	//			if (this == event.target && $(this).is('.open')) {
	//				$(this).children('ul').hide();
	//				$(this).removeClass('open');
	//				$(this).addClass('close');
	//			}
	//		});
	//	}
	//);

	$('a.category').each(
		function(category) {
			$(this).click(function(event) {
				if (this == event.target) {
					var selectedNodes = $(this).parents('li.node').map(function () {
		                  return this.id; 
	                }).get().join(',');
					setCookie('selectedNodes', selectedNodes, 30);
				}
			});
		}
	);

	if (window.location.search) {
		var catId = 'cat_' + $.urlParam('catId');
		var cookies = getCookie('selectedNodes');
		var selectedNodes = [];
		if (cookies) {
			selectedNodes = cookies.split(',');
		}
		
		if (!$.urlParam('catId') || selectedNodes.indexOf(catId) == 0) {
			$.each(selectedNodes, function(index, value) {
				$('#' + value).parent('ul').show();
				$('#' + value).addClass('selected_node');
			});
		} else {
			$('#' + catId).parents('ul').show();
			$('#' + catId).addClass('selected_node');
			$('#' + catId).parents('li.node').addClass('selected_node');
		}
	}
});
</script>
<div class="left_menu">
	<div class="left_menu_header"><?php echo $langs["home.category"];?></div>
	<div class="left_menu_content">
		<?php 
			include("../service/CategoryService.php");
			$selectedNode = $_GET["catId"];
			$branchId = getParentCategoryId($selectedNode);
			if($branchId == 0){
				$branchId = $selectedNode;
			}
			$temp = $branchId;
			while($temp != 0){
			$temp = getParentCategoryId($branchId);
				if($temp != 0){
				$branchId  = $temp;
				}
					
			}
			$rootCats = getAllParentCategory();

			function buildCategoryTree($parentCatId, $selectedNode){
				$childrentCats = getAllChildrenCategoryOf($parentCatId);
				//if ($childrentCats) {
					echo ("<ul>");
					while($row = mysql_fetch_array($childrentCats)){
						echo ("<li class='node' id='cat_" . $row['CAT_ID'] . "'><a class='category' href='" . (hasProduct($row['CAT_ID']) ? "product.php?&catId=" : "category.php?&catId=") . $row['CAT_ID'] . "'>" . $row['CAT_NAME_'.$_SESSION["lang"]] . "</a>");
						if(hasChild($row['CAT_ID'])&& $row['CAT_ID'] == $selectedNode){
							buildCategoryTree($row['CAT_ID'], $selectedNode);
						}else if(hasChild($row['CAT_ID'])&& $row['CAT_ID'] != $selectedNode && getParentCategoryId($selectedNode) == $row['CAT_ID']){
							buildCategoryTree($row['CAT_ID'], $selectedNode);
						}
						echo "</li>";
					}
					echo ("</ul>");
				//}
			}
		?>
		
		<ul>
			<?php 
				while ($row = mysql_fetch_array($rootCats)){
					
					echo ("<li class='node' id='cat_" . $row['CAT_ID'] . "'><a class='category' href='" . (hasProduct($row['CAT_ID']) ? "product.php?catId=" : "category.php?catId=") . $row['CAT_ID'] . "'>" . $row['CAT_NAME_'.$_SESSION["lang"]] . "</a>");
					if($row['CAT_ID'] == $branchId){
					buildCategoryTree($row['CAT_ID'], $selectedNode);
					}
					echo ("</li>");
				}
			?>
		</ul>
	</div>
</div>